<link href="Style.css" rel="stylesheet" type="text/css" />
<?php
session_start(); 

require("sql_functions.php");

if(isset($_SESSION['user']))
{	
	$user = $_SESSION['user'];
  echo "$hello";
	
	// New task or saving task
	if(isset($_POST['projectID']) && isset($_POST['MilestoneName']))
	{
		$pid = $_POST['projectID'];
		$mName = $_POST['MilestoneName'];
    $isNew = 0;
    $found = 1;
    
		//form info means editting
		if(isset($_POST['formName']) && isset($_POST['formDesc']) && isset($_POST['formDueDate']))
		{
    
			$tName = $_POST['formName'];
			$tDesc = $_POST['formDesc'];
			$tDueDate = makeSQLDate($_POST['formDueDate']);
      $email = $_POST['formAssignedTo'];
      
      if(isset($_POST['formIsComplete']))
      {
        $tIsComplete =  1;
      }
      else
      {
        $tIsComplete =  0;
      }
	$milestone = sql_getMilestone($pid, $mName);
	$mDueDate = $milestone['DueDate'];
	$interval = date_diff($mDueDate, $tDueDate);
      if($interval<=0)
	{
	      if(isset($_POST['isNew']) && $_POST['isNew'] == 1)
 	     {
	        $task = sql_createTask($pid, $mName, $tName, $tDesc, $tDueDate, $tIsComplete, $email);
	      }
	      else
	      {
	        if(isset($_POST['oldName']))
	        {
 	         $oldName = $_POST['oldName'];
	          $task = sql_editTask($pid, $mName, $oldName, $tName, $tDesc, $tDueDate, $tIsComplete, $email);
	        }      
	      }
	}
	else
	{
		echo "Task due date must be on or before milestone due date";
	}
      
      header("Location: task_info.php?projectID=$pid&MilestoneName=$mName&taskName=$tName");

		}

		//no form means new one
		else
		{			
      $isNew = 1;
			$tName = "";
			$tDesc = "";
			$tDueDate = makePHPDate("now");
			$tIsComplete = 0;
			$tAssignedTo = $user;
		}
		
	}
	
	else if( isset($_GET['projectID']) && isset($_GET['MilestoneName']) && isset($_GET['taskName']) )
	{		
    $found = 1;
    $isNew = 0;
		$pid = $_GET['projectID'];
		$mName = $_GET['MilestoneName'];
		$tName = $_GET['taskName'];

		$task = sql_getTask($pid, $mName, $tName);
    
    $tName = $task['TaskName'];
		$tDesc = $task['Description'];
		$tDueDate = makePHPDate($task['DueDate']);
		$tIsComplete = $task['IsClosed'];
		$tAssignedTo = $task['EmployeeEmail'];
    
    $oldName = $tName;
	}
	
	else
	{
    $found = 0;
		echo "Invalid Task";
	}
	
  if($found)
  {

	  $employees = sql_getEmployees();
	
	  echo "<html>"; 
	  echo "<head>"; 
	  echo "</head>"; 
	  echo "<body>";
	  echo "<p>Task Info"; 
	  echo "<p>Milestone: $mName";
	  echo "<form action=\"\" method=\"POST\">"; 
	  echo "<p>Name:";
	  echo "<input name=\"formName\" value=\"$tName\" />"; 
	  echo "</p>"; 
	  echo "<p>Description:";  
	  echo "<input name=\"formDesc\" value=\"$tDesc\" />"; 
	  echo "</p>";
	  echo "<p>Due Date:";  
	  echo "<input name=\"formDueDate\" value=\"$tDueDate\" />"; 


	  echo "<p>Assigned To:";
	  echo "<select name=\"formAssignedTo\"> ";
	  while($row = mysql_fetch_array($employees))
	  {
		  $email = $row['Email'];
		  $name = $row['FirstName'] . ' ' . $row['LastName'];
		  echo "<option value=\"$email\" ";
      if(!strcmp($email, $tAssignedTo))
      {
        echo " selected ";
      }
      echo "> $name </option>";

	  }
	  echo "</select>";

	  echo "<p>Complete?:"; 
	  echo "<input type=\"checkbox\" name=\"formIsComplete\" ";
    
    
    if($tIsComplete)
    {
      echo "checked ";
    }
    echo  "/>";
    
    echo "<input type=\"hidden\" name=\"oldName\" value=\"$oldName\"/>";
	  echo "<input type=\"hidden\" name=\"projectID\" value=\"$pid\"/>";
    echo "<input type=\"hidden\" name=\"MilestoneName\" value=\"$mName\"/>";
    echo "<input type=\"hidden\" name=\"isNew\" value=\"$isNew\"/>";

	  echo "<p><input type=\"submit\" name=\"save\" value=\"Save\" />"; 
	  echo "</form>";   
    echo "<p><a href=\"milestone_info.php?projectID=$pid&MilestoneName=$mName\">Milestone Info</a>"; 
  
	  echo "</body>"; 
	  echo "</html>"; 

  }
	
	
}
else
{
	echo "Must be logged in to view this page.";
}

?>
